package com.ahao.dao;

import com.ahao.model.User;
import com.ahao.utils.DBUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.util.List;

public class UserDao {
    QueryRunner queryRunner = new QueryRunner(DBUtils.getDataSource());

    /**
     * 根据用户名查询用户
     * @param username
     * @return
     * @throws Exception
     */
    public User selectUserByUsername(String username) throws Exception {
        return queryRunner.query("select * from user where username = ?", new BeanHandler<>(User.class), username);
    }

    /**
     * 查询所有用户
     * @return
     * @throws Exception
     */
    public List<User> selectAllUsers() throws Exception {
        return queryRunner.query("select * from user", new BeanListHandler<>(User.class));
    }

}
